package org.hxyjs.dao.impl;

import org.hxyjs.dao.NewsDao;
import org.hxyjs.entity.News;
import org.hxyjs.entity.Parents;
import org.hxyjs.mapper.NewsMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("newsDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class NewsDaoImpl implements NewsDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private NewsMapper newsMapper;
    public List<News>  getAllNewss() throws Exception{
        return newsMapper.selectByExample(null);
    }

    @Override
    public Integer deleteNewsByNno(String nno)  throws Exception{
        return newsMapper.deleteByPrimaryKey(nno);
    }

    @Override
    public Integer updateNewsByNno(News news)  throws Exception{
        return newsMapper.updateByPrimaryKey(news);
    }

    @Override
    public Integer addNews(News news)  throws Exception{
        news.setNno(initId());
        return newsMapper.insertSelective(news);
    }
    public String initId(){
        String maxNno = newsMapper.getMaxNno();
        return GenNumTools.initId("",3,maxNno);
    }

    @Override
    public List<News>  getNewsByPage(PageInfo pageInfo) throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int) newsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return newsMapper.getNewsByPage(map);
    }

    @Override
    public News selectNewsWithCarouselmap(String nno){
        return newsMapper.selectNewsWithCarouselmap(nno);
    }

}
